package com.controller;

import com.common.api.ApiResult;
import com.domain.FlyTask;
import com.service.FlyTaskService;
import com.websocket.MavlinkMessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

/**
 * @className: FlyTaskController
 * @description: TODO 类描述
 * @author: Kuangkeli
 * @date: 2024/07/09 10:33
 **/
@RestController
@RequestMapping("/flytask")
public class FlyTaskController {

    @Autowired
    private FlyTaskService flyTaskService;

    @Autowired
    private MavlinkMessageHandler mavlinkMessageHandler;

    @PostMapping("/add")
    public ApiResult sendFlyRectangleByP2PCmd(@RequestBody FlyTask flyTask) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        flyTask.setFlyStartDate(LocalDateTime.now());
        String timeId = simpleDateFormat.format(System.currentTimeMillis());
        flyTask.setTrackId(timeId);
        mavlinkMessageHandler.setTimeId(timeId);
        flyTaskService.save(flyTask);
        return ApiResult.success("任务添加成功");
    }

    @GetMapping("/getAll")
    public ApiResult get() {
        return ApiResult.success(flyTaskService.list());
    }

    @GetMapping("/test")
    public ApiResult test() {
        return ApiResult.success("连接成功");
    }
}
